package com.miaosha.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miaosha.demo.exception.GlobalException;
import com.miaosha.demo.mapper.OrderMapper;
import com.miaosha.demo.pojo.Order;
import com.miaosha.demo.pojo.SeckillGoods;
import com.miaosha.demo.pojo.SeckillOrder;
import com.miaosha.demo.pojo.User;
import com.miaosha.demo.service.IGoodsService;
import com.miaosha.demo.service.IOrderService;
import com.miaosha.demo.service.ISeckillGoodsService;
import com.miaosha.demo.service.ISeckillOrderService;
import com.miaosha.demo.utils.MD5Util;
import com.miaosha.demo.utils.UUIDUtil;
import com.miaosha.demo.vo.GoodsVo;
import com.miaosha.demo.vo.OrderDetailVo;
import com.miaosha.demo.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *

*
* @author xiaoshuaila
*/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private ISeckillGoodsService seckillGoodsService;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ISeckillOrderService seckillOrderService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Override
    public Order seckill(User user, GoodsVo goodsVo) {
        ValueOperations valueOperations =
                redisTemplate.opsForValue();
        SeckillGoods seckillGoods =
                seckillGoodsService.getOne(new QueryWrapper<SeckillGoods>()
                .eq("goods_id", goodsVo.getId()));
        seckillGoods.setStockCount(seckillGoods.getStockCount()-1);
        boolean result =
                seckillGoodsService.update(new UpdateWrapper<SeckillGoods>()
                .setSql("stock_count=stock_count-1")
                .eq("goods_id", goodsVo.getId())
                .gt("stock_count", 0));

        if(seckillGoods.getStockCount()<1){
            valueOperations.set("isStockEmpty:"+goodsVo.getId(),"0");
            return null;
        }
        Order order=new Order();
        order.setUserId(user.getId());
        order.setGoodsId(goodsVo.getId());
        order.setDeliveryAddrId(0L);
        order.setGoodsName(goodsVo.getGoodsName());
        order.setGoodsCount(1);
        order.setGoodsPrice(seckillGoods.getSeckillPrice());
        order.setOrderChannel(1);
        order.setStatus(0);
        order.setCreateDate(new Date());
        orderMapper.insert(order);
        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setUserId(user.getId());
        seckillOrder.setOrderId(order.getId());
        seckillOrder.setGoodsId(goodsVo.getId());
        seckillOrderService.save(seckillOrder);
        redisTemplate.opsForValue().set("order:"+user.getId()+":"+goodsVo.getId(),seckillOrder);
        return order;
    }

    @Override
    public OrderDetailVo datail(Long orderId) {
        if(orderId==null)
            throw new GlobalException(RespBeanEnum.ORDER_NOT_EXIST);
        Order order=orderMapper.selectById(orderId);
        GoodsVo goodsVo =
                goodsService.findGoodsByGoodsId(order.getGoodsId());
        OrderDetailVo detailVo=new OrderDetailVo();
        detailVo.setOrder(order);
        detailVo.setGoodsVo(goodsVo);
        return detailVo;
    }

    @Override
    public String createPath(User user, Long goodsId) {
        String str=MD5Util.md5(UUIDUtil.uuid()+"123456");
        redisTemplate.opsForValue().set("seckillPath:"+user.getId()+":"+goodsId,str,60, TimeUnit.SECONDS);
        return str;
    }

    @Override
    public boolean checkPath(User user, Long goodsId,String path) {
        if(user==null||goodsId<0|| StringUtils.isEmpty(path))
        return false;
        String redisPath = (String) redisTemplate.opsForValue().get(
                "seckillPath:" + user.getId() + ":" + goodsId);
        return redisPath.equals(path);
    }

    @Override
    public boolean checkCaptcha(User user, Long goodsId,
                                String captcha) {
        if(StringUtils.isEmpty(captcha)||user==null||goodsId<0)return false;
        String redisCaptcha = (String) redisTemplate.opsForValue().get(
                "captcha:" + user.getId() + ":" + goodsId);
        return captcha.equals(redisCaptcha);
    }
}
